﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Arquitetura.UI.WebApp.SecretariaServiceReference;

namespace Arquitetura.UI.WebApp
{
    public partial class AlunoListar : System.Web.UI.Page
    {
        #region Constante(s)
        protected const string odsAlunoFiltroKey = "alunoFiltro";
        #endregion Constante(s)

        #region Atributo(s)
        private SecretariaServiceClient m_secretariaServiceClient;
        #endregion Atributo(s)

        #region Propriedade(s)
        private AlunoManutencao.Operacao? OperacaoCorrenteManutencao
        {
            get
            {
                return Session[AlunoManutencao.sesOperacaoKey] as AlunoManutencao.Operacao?;
            }

            set
            {
                if (value != null)
                {
                    Session[AlunoManutencao.sesOperacaoKey] = value;
                }
                else
                {
                    Session.Remove(AlunoManutencao.sesOperacaoKey);
                }
            }
        }

        private int? IdAlunoParametro
        {
            get
            {
                return Session[AlunoManutencao.sesIdAlunoParametroKey] as int?;
            }

            set
            {
                if (value != null)
                {
                    Session[AlunoManutencao.sesIdAlunoParametroKey] = value;
                }
                else
                {
                    Session.Remove(AlunoManutencao.sesIdAlunoParametroKey);
                }
            }
        }
        #endregion Propriedade(s)

        #region Evento(s)
        protected void Page_Preinit(object sender, EventArgs e)
        {
            m_secretariaServiceClient = new SecretariaServiceClient();
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            ((IDisposable)m_secretariaServiceClient).Dispose();
        }

        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            grvAlunos.DataBind();
            grvAlunos.Visible = true;
        }

        protected void btnIncluir_Click(object sender, EventArgs e)
        {
            IdAlunoParametro = null;
            OperacaoCorrenteManutencao = AlunoManutencao.Operacao.Incluir;
            Response.Redirect("~/AlunoManutencao.aspx");
        }

        protected void btnLimpar_Click(object sender, EventArgs e)
        {
            txtNome.Text = string.Empty;
            txtSobrenome.Text = string.Empty;
            grvAlunos.Visible = false;
        }

        protected void grvAlunos_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvAlunos.PageIndex = e.NewPageIndex;
            grvAlunos.DataBind();
        }

        protected void grvAlunos_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Alterar":
                    {
                        IdAlunoParametro = Convert.ToInt32(e.CommandArgument);
                        OperacaoCorrenteManutencao = AlunoManutencao.Operacao.Alterar;
                        Response.Redirect("~/AlunoManutencao.aspx");
                    } break;

                case "Excluir":
                    {
                        Aluno alunoExcluido = new Aluno() { Id = Convert.ToInt32(e.CommandArgument) };
                        Result result = m_secretariaServiceClient.ExcluirAluno(alunoExcluido);
                        if (result.Success)
                        {
                            grvAlunos.DataBind();
                            grvAlunos.Visible = true;
                        }
                        else
                        {
                            Response.Write("Falha");
                        }
                    } break;
            }
        }

        protected void odsAlunos_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            AlunoHelper ah = new AlunoHelper(m_secretariaServiceClient);
            e.ObjectInstance = ah;
        }

        protected void odsAlunos_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters[odsAlunoFiltroKey] = CarregarFiltro();
        }

        protected void odsAlunos_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            Result result = e.OutputParameters["resultado"] as Result;
            if (result != null && !result.Success)
            {
                Response.Write(result.ConsolidateResult("<br />"));
            }
        }
        #endregion Evento(s)

        #region Método(s)
        private Aluno CarregarFiltro()
        {
            Aluno aluno = new Aluno();
            aluno.Nome = txtNome.Text;
            aluno.Sobrenome = txtSobrenome.Text;
            return aluno;
        }
        #endregion Método(s)
    }
}
